package com.example.flex.controller;

import com.example.flex.domain.RoleEntity;
import com.example.flex.utils.response.BaseResponse;
import com.example.flex.service.RoleService;
import com.example.flex.utils.common.PageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
import java.util.List;
import java.util.Arrays;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 角色表控制层
 * @Author: 不是菜鸡爱编程
 * @Date:
 * @Description：
 */
@RestController
@Api(tags = "角色表接口")
@RequestMapping("role")
public class RoleController{

    @Autowired
    private RoleService roleService;

    /**
     * 保存
     *
     * @param roleEntity 角色表
     * @return BaseResponse
     */
    @PostMapping("/save")
    @ApiOperation("保存角色表")
    public BaseResponse<?> save(@RequestBody @ApiParam("角色表") RoleEntity roleEntity){
        boolean saveFlag = roleService.save(roleEntity);
        if(saveFlag){
            return BaseResponse.success("保存成功");
        }else{
            return BaseResponse.error("保存失败");
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return BaseResponse
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据主键查询角色表")
    public BaseResponse<?> info(@PathVariable @ApiParam("角色表主键") Integer id){
        return BaseResponse.success(roleService.getById(id),"查询成功");
    }

    /**
     * 列表查询
     *
     * @return BaseResponse
     */
    @PostMapping("/list")
    @ApiOperation("列表查询角色表")
    public BaseResponse<?> list(@RequestBody @ApiParam("角色表分页参数") PageEntity<RoleEntity> roleEntityPage){
        RoleEntity roleEntity = roleEntityPage.getData();
        QueryWrapper queryWrapper = getQueryWrapper(roleEntity);
        return BaseResponse.success(roleService.page(new Page<>(roleEntityPage.getPageNum(),roleEntityPage.getPageSize()),queryWrapper),"查询成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return BaseResponse
     */
    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除角色表")
    public BaseResponse<?> delete(@PathVariable @ApiParam("角色表主键(列表)") Integer[] ids){
        return BaseResponse.success(roleService.removeByIds(Arrays.asList(ids)),"删除成功");
    }


    /**
     * 修改
     *
     * @param roleEntity 角色表
     * @return BaseResponse
     */
    @PutMapping("/edit")
    @ApiOperation("修改角色表")
    public BaseResponse<?> edit(@RequestBody @ApiParam("角色表") RoleEntity roleEntity){
        return BaseResponse.success(roleService.updateById(roleEntity),"修改成功");
    }

    /**
     * 获取queryWrapper
     *
     * @param roleEntity 角色表
     * @return {@link QueryWrapper}
     */
    private QueryWrapper getQueryWrapper(RoleEntity roleEntity){
        return QueryWrapper.create()
                    .eq(RoleEntity::getId,roleEntity.getId(),roleEntity.getId()!=null)
                    .eq(RoleEntity::getName,roleEntity.getName(),roleEntity.getName()!=null)
                    .eq(RoleEntity::getRoleKey,roleEntity.getRoleKey(),roleEntity.getRoleKey()!=null)
                    .eq(RoleEntity::getStatus,roleEntity.getStatus(),roleEntity.getStatus()!=null)
                    .eq(RoleEntity::getDelFlag,roleEntity.getDelFlag(),roleEntity.getDelFlag()!=null)
                    .eq(RoleEntity::getCreateBy,roleEntity.getCreateBy(),roleEntity.getCreateBy()!=null)
                    .eq(RoleEntity::getCreateTime,roleEntity.getCreateTime(),roleEntity.getCreateTime()!=null)
                    .eq(RoleEntity::getUpdateBy,roleEntity.getUpdateBy(),roleEntity.getUpdateBy()!=null)
                    .eq(RoleEntity::getUpdateTime,roleEntity.getUpdateTime(),roleEntity.getUpdateTime()!=null)
                    .eq(RoleEntity::getRemark,roleEntity.getRemark(),roleEntity.getRemark()!=null);
    }
}